package com.snowfeel.sfmall.sys.controller;

import com.snowfeel.sfmall.core.param.IdParam;
import com.snowfeel.sfmall.core.param.IdsParam;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.core.vo.Option;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.dto.SysDeptParam;
import com.snowfeel.sfmall.sys.query.SysDeptQuery;
import com.snowfeel.sfmall.sys.service.SysDeptService;
import com.snowfeel.sfmall.sys.vo.SysDeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author guomenghuai
 * @date 2024/9/7
 * @Description 部门管理
 */
@Api(tags = "部门接口")
@RestController
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService deptService;

    @ApiOperation(value = "获取部门列表")
    @PostMapping("page")
    public JsonResult<PageVo<SysDeptVo>> page(@RequestBody SysDeptQuery queryParams) {
        PageVo<SysDeptVo> pageVO = deptService.page(queryParams);
        return JsonResult.success(pageVO);
    }

    @ApiOperation(value = "获取部门下拉选项")
    @GetMapping("/options")
    public JsonResult<List<Option>> listDeptOptions() {
        List<Option> list = deptService.listDeptOptions();
        return JsonResult.success(list);
    }

    @ApiOperation(value = "获取部门详情")
    @PostMapping("/detail")
    public JsonResult<SysDeptVo> detail(@RequestBody IdParam param) {
        SysDeptVo deptVO = deptService.detail(param);
        return JsonResult.success(deptVO);
    }

    @ApiOperation(value = "新增部门")
    @PostMapping("/create")
    public JsonResult create(
            @Valid @RequestBody SysDeptParam deptParam) {
        Long id = deptService.create(deptParam);
        return JsonResult.success(id);
    }

    @ApiOperation(value = "修改部门")
    @PostMapping(value = "/update/{id}")
    public JsonResult update(@PathVariable Long id, @Valid @RequestBody SysDeptParam formData) {
        Long deptId = deptService.update(id, formData);
        return JsonResult.success(deptId);
    }

    @ApiOperation(value = "删除部门")
    @PostMapping("/deleteBatch")
    public JsonResult deleteBatch(@ApiParam("部门ID集合") @RequestBody IdsParam param) {
        boolean result = deptService.deleteBatch(param);
        return JsonResult.success(result);
    }

}
